package com.itheima.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.springboot.common.Result;
import com.itheima.springboot.controller.dto.UserAddressDto;
import com.itheima.springboot.entity.User;
import com.itheima.springboot.mapper.UserMapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: UserController
 * @Description:
 * @Author: lanshou666
 * @Create: 2022-07-21 22:23
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserMapper userMapper;

    /*
     * @param user:
     * @return com.itheima.springboot.common.Result<?>
     * @author lanshou666
     *  保存用户
    */
    @PostMapping()
    public Result<?> save(@RequestBody User user){
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        userMapper.insert(user);
        return Result.success();
    }

    @PutMapping()
    public Result<?> update(@RequestBody User user){
        userMapper.updateById(user);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        userMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping
    public Result<?> find(@RequestParam(defaultValue = "1") Integer currentPage,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>lambdaQuery().like(User::getNickName, search);
        if (StringUtils.isNotBlank(search)){
            queryWrapper.like(User::getNickName,search);
        }
        Page<User> userPage = userMapper.selectPage(new Page<>(currentPage, pageSize),queryWrapper);
        return Result.success(userPage);
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id){
        return Result.success(userMapper.selectById(id));
    }

    @GetMapping("/bookList/{id}")
    public Result<?> getBookList(@PathVariable Long id){
        User user = userMapper.getBookList(id);
        return Result.success(user.getBookList());
    }

    @PostMapping("/login")
    public Result<?> login(@RequestBody User user){
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        if (res==null) {
            return Result.error("-1","用户名或密码错误");
        }
        return Result.success(res);
    }

    @PostMapping("/register")
    public Result<?> register(@RequestBody User user){
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        if (res!=null) {
            return Result.error("-1","用户名重复");
        }
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        userMapper.insert(user);
        return Result.success();
    }

    @GetMapping("/count")
    public Result<?> count(){
        return Result.success(userMapper.countAddress());
    }

}
